package com.atguigu.eduorder.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduorder.service.PayLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author faiz
 * @since 2021-05-19
 */
@Api(tags = "微信支付")
@RestController
@RequestMapping("/eduorder/paylog")
//@CrossOrigin
public class PayLogController {

    @Autowired
    private PayLogService payLogService;

    @ApiOperation(value = "生成微信支付二维码")
    @GetMapping("createNative/{orderNo}")
    public R createNative(@PathVariable("orderNo") String orderNo) {
        //返回相关信息，包含二维码地址,还有其他的一些信息
        Map<String, Object> map = payLogService.createNative(orderNo);
        System.out.println("返回二维码map集合:" + map);
        return R.ok().data(map);
    }

    @ApiOperation("查询订单支付的状态")
    @GetMapping("queryPayStatus/{orderNo}")
    public R queryPayStatus(@PathVariable("orderNo") String orderNo) {
        Map<String, String> map = payLogService.queryPayStatus(orderNo);
        System.out.println("查询订单状态map集合:" + map);
        if (map == null) {
            R.error().message("支付出错！");
        }
        if (map.get("trade_state").equals("SUCCESS")) { //支付成功
            //像支付表中添加记录,同时更新状态
            payLogService.updateOrderStatus(map);
            return R.ok().message("支付成功");
        }
        return R.ok().code(25000).message("支付中");
    }
}

